//
//  RBTextView.m
//  Culture
//
//  Created by Lan on 2017/8/16.
//  Copyright © 2017年 SummerTea. All rights reserved.
//

#import "RBTextView.h"
#import "Masonry.h"
#import "RBDefine.h"
#import <YYCategories/NSString+YYAdd.h>
#import "RanTool.h"

@interface RBTextView ()

@property(nonatomic, strong) UILabel *label;

@property(nonatomic, assign) BOOL autoGrow;
@property(nonatomic, assign) CGFloat conWidth;
@property(nonatomic, assign) CGFloat minHeight;
@property(nonatomic, assign) CGFloat maxHeight;

@end

@implementation RBTextView

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.text = self.text;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    self.label.hidden = text.length;
    [self adjustHeight];
    if (self.textChangeBlock)
    {
        self.textChangeBlock(self.text);
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self adjustHeight];
}

#pragma mark - Private

- (void)setup
{
    kRBIgnoreScrollViewInsetsAdjust(self);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    self.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)adjustHeight
{
    if (!self.autoGrow || !self.superview) return;
    
    CGFloat height = [self sizeThatFits:CGSizeMake(self.conWidth, CGFLOAT_MAX)].height;
    //防止滑动
    if (self.contentSize.height > height)
    {
        self.contentSize = CGSizeMake(self.contentSize.width, height);
    }
    self.scrollEnabled = height > self.maxHeight;
    height = MAX(height, self.minHeight);
    height = MIN(height, self.maxHeight);
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
}

#pragma mark - Public

- (void)setPlaceHolder:(NSString *)placeHolder
{
    if (placeHolder)
    {
        self.label.text = placeHolder;
    }
}

- (void)setPlaceHolderFontSize:(CGFloat)placeHolderFontSize
{
    if (placeHolderFontSize)
    {
        self.label.font = [UIFont systemFontOfSize:placeHolderFontSize];
    }
}

- (void)setPlaceHolderColor:(UIColor *)placeHolderColor
{
    if (placeHolderColor)
    {
        self.label.textColor = placeHolderColor;
    }
}

- (void)setAttPlaceHolder:(NSAttributedString *)attPlaceHolder
{
    self.label.attributedText = attPlaceHolder;
}

- (void)enableAutoGrowWithConsWidth:(CGFloat)width minHeight:(CGFloat)minHeight maxHeight:(CGFloat)maxHeight
{
    //适配IQKeyboardManager
    self.scrollEnabled = NO;
    
    self.autoGrow = YES;
    self.conWidth = width;
    self.minHeight = minHeight;
    self.maxHeight = maxHeight;
}

#pragma mark - Event

- (void)textChanged: (NSNotification *)noti
{
    if (noti.object == self)
    {
        self.label.hidden = self.text.length;
        if ((!self.markedTextRange || self.markedTextRange.isEmpty))
        {
            if (self.maxTextLength > 0 && self.text.length > self.maxTextLength)
            {
                self.text = [self.text substringToIndex:self.maxTextLength];
            }
            else
            {
                [self adjustHeight];
                if (self.textChangeBlock)
                {
                    self.textChangeBlock(self.text);
                }
            }
        }
    }
}

#pragma mark - Getter

- (UILabel *)label
{
    if (!_label)
    {
        self.label = ({
            UILabel *view = [UILabel new];
            view.textColor = [UIColor lightGrayColor];
            view.font = self.font;
            view.numberOfLines = 0;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                CGFloat hPadding = 5;
                CGFloat vPadding = 7;
                make.left.mas_equalTo(hPadding);
                make.right.mas_equalTo(-hPadding);
                make.top.mas_equalTo(vPadding);
            }];
            view;
        });
    }
    return _label;
}

@end
